
#ifndef _AGENT__TASK_SCHEDULER_H__INCLUDED_
#define _AGENT__TASK_SCHEDULER_H__INCLUDED_

#include "Utility/ErrorCode.h"
#include <memory>

namespace nebula { namespace utility {

template <typename helper_type>
class TaskSchedulerImpl;

template <typename helper_type>
class TaskScheduler
{
public:
	typedef NEBULA_ERROR_CODE (helper_type::*task_processor)(bool &repeat_immediately);

public:
	TaskScheduler(const char *name=0);

public:
	NEBULA_ERROR_CODE start(
			helper_type *helper,
			task_processor proc,
			unsigned int idle_sleep,//ms
			unsigned int min_thread_count,
			unsigned int max_thread_count);
	NEBULA_ERROR_CODE stop();

	// asynchronously notify the thread pool to stop, you still have
	// to call 'stop' to recycle the resource
	NEBULA_ERROR_CODE notify_to_stop();

	//should not be called before start()
	NEBULA_ERROR_CODE set_thread_count_range(
			unsigned int min_thread_count,
			unsigned int max_thread_count);

private:
	std::auto_ptr<TaskSchedulerImpl<helper_type> > pImpl;

private:
    friend class TaskSchedulerTest;//For UT only
};

}} // namespace nebula::utility

#include "TaskSchedulerImpl.h"
#endif // _AGENT__TASK_SCHEDULER_H__INCLUDED_

